package com.pc.controller;

import com.pc.service.MoneyService;
import com.pc.utils.Result;
import com.pc.utils.aop.OperLog;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("money")
public class MoneyController {
    @Resource
    private MoneyService moneyService;

    //得到用户余额
    @GetMapping("/getMoney/{username}")
    @OperLog(operModul = "余额模块",operType = "查询",operDesc = "得到用户余额")
    public Result getMoney(@PathVariable String username){
        Integer userMoney = moneyService.getUserMoney(username);
        if(userMoney>=0)
            return Result.success("查询成功",userMoney);
        else
            return Result.fail("获取余额失败");
    }
    //充值
    @PostMapping("/addMoney")
    @OperLog(operModul = "余额模块",operType = "增加",operDesc = "用户充值")
    public Result addMoney(@RequestBody Object M){
        Map entity = (Map)M;
        String username = entity.get("username").toString();
        String a = entity.get("upmoney").toString();
        int addmoney =Integer.valueOf(a);
        boolean b = moneyService.addMoney(username, addmoney);
        if(b)
            return Result.success("充值成功",true);
        else
            return Result.fail("充值失败",false);
    }

    @PostMapping("/subMoney")
    @OperLog(operModul = "余额模块",operType = "修改",operDesc = "用户支付,扣款")
    public Result subMoney(@RequestBody Object M){
        Map m = (Map) M;
        String username = m.get("username").toString();
        String money = m.get("submoney").toString();
        int submoney=Integer.valueOf(money);
        boolean b = moneyService.subMoney(username, submoney);
        if(b)
            return Result.success("付款成功",true);
        else
            return Result.fail("付款失败",false);
    }
}
